<?php

namespace app\admin\controller;

use think\Controller;

class Index extends Controller
{
    //重复登录过滤
    public function _initialize()
    {
        if (session('?login.id')){
            $this -> redirect('admin/home/home');
        }
    }

    //后台登录
    public function login()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $result = model('common/Admin') -> login($data);
            if ($result == 1){
                $this -> success('登录成功！','admin/home/home');
            } else {
                $this -> error($result);
            }
        }
        $webname = model('common/System') -> field('webname') -> find();
        $data = [
            'webname' => $webname
        ];
        $this -> assign($data);
        return view();
    }

    //发送验证码
    public function forget()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $result = model('common/Admin') -> forget($data);
            if ($result == 1){
                $this -> success('验证码发送成功！'.session('code.num'));
            } else {
                $this -> error($result);
            }
        }
        $webname = model('common/System') -> field('webname') -> find();
        $data = [
            'webname' => $webname
        ];
        $this -> assign($data);
        return view();
    }

    //忘记密码
    public function setpass()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $result = model('common/Admin') -> setpass($data);
            if ($result == 1){
                $this -> success('密码修改成功！','admin/index/login');
            } else {
                $this -> error($result);
            }
        }
    }


}
